package io;

public class ClientServer
{
	public static final int DEFAULT_PORT = 3450;
	private int _port;
	private Thread _serverThread;
	private Thread _clientThread;
	private Server _server;
	private Client _client;
	private ListenerManager _manager;
	
	public ClientServer(ListenerManager manager)
	{
		this(DEFAULT_PORT, manager);
	}
	
	public ClientServer(int port, ListenerManager manager)
	{
		_port = port;
		_manager = manager;
	}
	
	public void launchAsServer()
	{
		_server = new Server(_port, _manager);
		_serverThread = new Thread(_server);
		_serverThread.start();
	}
	
	public void launchAsClient()
	{
		_client = new Client(_port, _manager);
		_clientThread = new Thread(_client);
		_clientThread.start();
	}

	public void Send(Object message)
	{
		if (_server != null)
		{
			_server.getCommunicator().Send(message);	
		}
		if (_client != null)
		{
			_client.getCommunicator().Send(message);
		}
	}
}